package com.student.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.student.dao.StatisticsDao;
import com.student.model.Campus;
import com.student.model.Colleges;

@Component("statisticsDao")
public class StatisticsDaoImpl extends HibernateDaoImpl implements
		StatisticsDao {

	public Double getStudentCount(String collegeName, Boolean isRegister)
			throws Exception {
		try {
			String hql = "SELECT COUNT(*) FROM Student s WHERE 1=1";
			if (collegeName != null) {
				hql += " AND s.profession.colleges.collegesName='"
						+ collegeName + "'";
			}
			if (isRegister != null) {
				hql += " AND s.isRegister='" + isRegister + "'";
			}
			Query query = getSession().createQuery(hql);
			if (query.iterate().next() != null) {
				return new Double(query.iterate().next() + "");
			}
			return new Double(0);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	public List<Colleges> getAllCollege() throws Exception {
		String hql = "FROM Colleges c ORDER BY c.collegeId";
		return find(hql);
	}

	public List<Campus> getAllCampus() throws Exception {
		String hql = "FROM Campus c ORDER BY c.campusId";
		return find(hql);
	}

	public List<Colleges> getAllCollege(String campusName) throws Exception {
		String hql="FROM Colleges c WHERE c.campus.campusName='"+campusName+"'";
		return find(hql);
	}

	public Double getCampusStudentCount(String campusName, Boolean isRegister)
			throws Exception {
		try {
			String hql = "SELECT COUNT(*) FROM Student s WHERE 1=1";
			if (campusName != null) {
				hql += " AND s.profession.colleges.campus.campusName='"
						+ campusName + "'";
			}
			if (isRegister != null) {
				hql += " AND s.isRegister='" + isRegister + "'";
			}
			Query query = getSession().createQuery(hql);
			if (query.iterate().next() != null) {
				return new Double(query.iterate().next() + "");
			}
			return new Double(0);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	public Double getStudentCount(String campusName, String collegeName,
			Boolean isRegister, Boolean isCampus) throws Exception {
		try {
			String hql = "SELECT COUNT(*) FROM Student s WHERE 1=1";
			if (isRegister != null) {
				hql += " AND s.isRegister='" + isRegister + "'";
			}
			if (isCampus) {
				if (campusName != null) {
					hql += " AND s.profession.colleges.campus.campusName='"
							+ campusName + "'";
				}
			} else {
				if (collegeName != null) {
					hql += " AND s.profession.colleges.collegesName='"
							+ collegeName + "'";
				}
			}
			Query query = getSession().createQuery(hql);
			if (query.iterate().next() != null) {
				return new Double(query.iterate().next() + "");
			}
			return new Double(0);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

}
